@(products: com.avaje.ebean.Page[Product], producer: String, priceStart: String, priceEnd: String, currentSortBy: String, currentOrder: String, currentFilter: String)

@link(newPage:Int, newSortBy:String) = @{
    
    var sortBy = currentSortBy
    var order = currentOrder
    
    if(newSortBy != null) {
        sortBy = newSortBy
        if(currentSortBy == newSortBy) {
            if(currentOrder == "asc") {
                order = "desc"
            } else {
                order = "asc"
            }
        } else {
            order = "asc"
        }
    }
    
    // Generate the link
    routes.Admin.product(newPage, producer, priceStart, priceEnd, sortBy, order, currentFilter)
}

@header(key:String, title:String) = {
    <th class="@key.replace(".","_") header @if(currentSortBy == key) @{if(currentOrder == "asc") "headerSortDown" else "headerSortUp"}">
        <a href="@link(0, key)">@title</a>
    </th>
}

    <div id="actions">
        
        <form action="@link(0, "name")" method="GET">
            <input type="search" id="searchbox" name="f" value="@currentFilter" placeholder="Filter by title...">
            <input type="search" id="searchbox" name="pr" value="@producer" placeholder="Filter by producer...">
            <input type="submit" id="searchsubmit" value="Filter" class="btn primary">
        </form>
        
    </div>

	@if(products.getTotalRowCount == 0) {
        <div class="well">
            <em>Nothing to display</em>
        </div>
        
    } else {
        
        <table class="computers zebra-striped">
            <thead>
                <tr>
                    @header("title", "Title")
                    @header("producer", "Producer")
                    @header("price", "Price")
                    @header("quantity", "Quantity")
                    <th>
                    	Options
                    </th>
                </tr>
            </thead>
            <tbody>

                @for(product <- products.getList) {
                    <tr>
                        <td><a href="">@product.title</a></td>
                        <td>
                        	@product.producer
                        </td>
                        <td>
                        	@product.price
                        </td>
                        <td>
                            @product.quantity
                        </td>
                        <td>
                        	<a href="@routes.Admin.deleteProduct(product.id)">Delete</a>
                        </td>
                        <td>
                        	<a href="@routes.Admin.productPage(product.id)">Edit</a>
                        </td>
                        <td>
                        	<form action = "@routes.CartControl.addToCart()" method = "GET">
                        		<input type = "hidden" name = "productId" value = "@product.id" />
                        		<input type = "quantity" name = "quantity" value = "1" />
                        		<button type = "submit">Submit</button>
                        	</form>
                        </td>
                    </tr>
                }

            </tbody>
        </table>

        <div id="pagination" class="pagination">
            <ul>
                @if(products.hasPrev) {
                    <li class="prev">
                        <a href="@link(products.getPageIndex - 1, null)">&larr; Previous</a>
                    </li>
                } else {
                    <li class="prev disabled">
                        <a>&larr; Previous</a>
                    </li>
                }
            	@for(i <- 0 until products.getTotalPageCount()){
            		<li><a href = "@link(i,null)">@(i+1)</a></li>
            	}
                @if(products.hasNext) {
                    <li class="next">
                        <a href="@link(products.getPageIndex + 1, null)">Next &rarr;</a>
                    </li>
                } else {
                    <li class="next disabled">
                        <a>Next &rarr;</a>
                    </li>
                }
            </ul>
        </div>
}